package com.jeomo.mem.starter.service.impl;

import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.common.core.constant.SearchParam;
import com.jeomo.common.core.constant.Sort;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.exception.BusinessException;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.masterdata.starter.service.ILevelService;
import com.jeomo.mem.starter.entity.MemberLevelExeUpdate;
import com.jeomo.mem.starter.entity.MemberPointsAdjust;
import com.jeomo.mem.starter.entity.MemberPointsUpdate;
import com.jeomo.mem.starter.enums.MemberPointsSrcEnum;
import com.jeomo.mem.starter.mapper.MemPointsAdjustMapper;
import com.jeomo.mem.starter.service.IMemPointsAdjustService;
import com.jeomo.mem.starter.service.IMemberCardService;
import com.jeomo.mem.starter.sysoption.PointsValiditySysOption;
import com.jeomo.shiro.util.LoginSessionUtil;
import com.jeomo.shiro.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 积分调整
 *
 * @author:Jackpot
 * @create 2020-12-23 17:41
 * @description:
 */
@Service
@Transactional
public class MemPointsAdjustService  extends BaseServiceImpl<MemPointsAdjustMapper, MemberPointsAdjust>
        implements IMemPointsAdjustService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String CARD_NO = "card_no";
    private final String MEMBER_PHONE = "member_phone";
    private final String TIME_FROM = "time_from";
    private final String TIME_TO = "time_to";
    private final String HANDLER = "handler";
    private final String HANDLE_TIME = "handle_time";

    @Resource
    private MemPointsAdjustMapper memPointsAdjustMapper;
    @Resource
    private ILevelService levelService;
    @Resource
    private IMemberCardService memberCardService;
    @Resource
    private MemberLevelExeService memberLevelExeService;
    @Resource
    private PointsValiditySysOption pointsValiditySysOption;

    @Override
    public boolean insertPointsAdjust(MemberPointsAdjust memberPointsAdjust) {

        Integer pointsOld = memberPointsAdjust.getPointsOld();
        Integer pointsChange = memberPointsAdjust.getPointsChange();
        if (pointsOld + pointsChange<0){
            throw new BusinessException("调整后的积分小于0, 不合法!");
        }
        memberPointsAdjust.setOrg(LoginSessionUtil.getCurrentLoginInfo().getOrgCode());
        memberPointsAdjust.setHandler(UserUtil.getUserName());
        memberPointsAdjust.setHandleTime(LocalDateTime.now());
        LocalDate expiredTime = memberPointsAdjust.getExpiredTime();
        if (expiredTime==null && pointsChange >0) expiredTime = pointsValiditySysOption.getValidityDate(LocalDate.now());
        memberPointsAdjust.setExpiredTime(expiredTime);
        int insert = memPointsAdjustMapper.insert(memberPointsAdjust);
        //积分变动
        MemberPointsUpdate memberPointsUpdate = BeanCopyUtil.copyProperties(memberPointsAdjust, MemberPointsUpdate.class);
        memberPointsUpdate.setSrcCode(memberPointsAdjust.getCode());
        memberPointsUpdate.setSrcTypeCode(MemberPointsSrcEnum.ADJUST.getCode());
        memberCardService.updateMemberPoints(memberPointsUpdate);

        MemberLevelExeUpdate memberLevelExeUpdate = BeanCopyUtil.copyProperties(memberPointsUpdate, MemberLevelExeUpdate.class);
        memberLevelExeUpdate.setAcquirePoints(pointsChange);
        memberLevelExeService.updateLevelExe(memberLevelExeUpdate);
        return insert > 0 ;
    }

    @Override
    public PageResponseDto pageSearch(PageQueryDto dto) {

        Map<String, Object> searchParams = dto.getSearchParams();
        if(searchParams!=null){
            Map<String, Object> params = new HashMap<>(8);
            params.put(SearchParam.SEARCH_RIGHT_LIKE+CARD_NO,searchParams.get(CARD_NO));
            params.put(SearchParam.SEARCH_EQ+MEMBER_PHONE,searchParams.get(MEMBER_PHONE));
            params.put(SearchParam.SEARCH_EQ+HANDLER,searchParams.get(HANDLER));
            params.put(SearchParam.SEARCH_GE+HANDLE_TIME,searchParams.get(TIME_FROM));
            params.put(SearchParam.SEARCH_LE+HANDLE_TIME,searchParams.get(TIME_TO));
            dto.setSearchParams(params);
        }
        Map<String, String> sort = new HashMap<>(8);
        sort.put(HANDLE_TIME, Sort.DESC);
        dto.setSorts(sort);
        PageResponseDto pageResponseDto = super.pageSearch(dto);
        Map<String, String> levelMap = levelService.queryCache();
        List<?> items = pageResponseDto.getItems();
        for (Object item : items) {
            MemberPointsAdjust detail = (MemberPointsAdjust)item;
            detail.setLevelName(levelMap.get(detail.getLevelCode()));
        }
        return pageResponseDto;
    }

    @Override
    public MemberPointsAdjust getOneDetail(String code) {
        return memPointsAdjustMapper.selectById(code);
    }

}
